package com.snr.userhistoryprocessor;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.snr.userhistoryprocessor.dao.ReadingLogDao;
import com.snr.userhistoryprocessor.pojo.ReadingLog;
import com.snr.userhistoryprocessor.pojo.User;
import com.snr.userhistoryprocessor.urlprocessor.TextExtract;
import com.snr.userhistoryprocessor.urlprocessor.UseDemo;

/**
 * Hello world!
 *
 */
public class App 
{
	private static Logger logger = Logger.getLogger(App.class);
    public static void main( String[] args )
    {
    	ApplicationContext ac=new ClassPathXmlApplicationContext("/applicationContext.xml");
    	ReadingLogDao readingLogDao = (ReadingLogDao) ac.getBean("readingLogDao");
    	User user = new User();
    	user.setId(1);;
    	List<ReadingLog> list = readingLogDao.getReadingLogs(user);
    	logger.info(list.toString());
    	System.out.println(list.size());
    	Iterator<ReadingLog> iterator = list.iterator();
    	while (iterator.hasNext()) {
    		
			ReadingLog readingLog = (ReadingLog) iterator.next();
			logger.info("deal url:"+readingLog.getUrl());
			String content;
			try {
				content = UseDemo.getHTML(readingLog.getUrl());
				//System.out.println(content);
				String content2 = TextExtract.parse(content);
				System.out.println(content2);
				if (content2.trim().length()==0) {
					logger.info("no info:"+readingLog.getUrl());
					//continue;
				}
				BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\Data\\urlcontent\\"+user.getId()+"_"+readingLog.getDate()+".txt")));
		    	br.write(content2);
		    	br.flush();
		    	br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\Data\\urlcontent\\"+user.getId()+"_"+readingLog.getDate()+".html")));
		    	br.write(content);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.info("deal url wrong:"+e);
			}
			
		}
        System.out.println( "Hello World!" );
    }
}
